package com.yonyou.pmclouds.plan.utils;

import com.yonyou.pmclouds.plan.entity.ProjectScheduleBoardVO;
import com.yonyou.pmclouds.plan.entity.ScheduleItemVO;
import com.yonyou.pmclouds.plan.entity.ScheduleVO;

import java.util.List;

/**
 * @Description:
 * @Author: zhangzhonghao
 * @Date: 2020-03-15
 */
public class ScheduleProgressCalculateUtil {

    /**
     * 根据子级计算父级进度百分比：
     * 计算方式： 父级进度 = (∑（子级进度 * 工期）i) / (∑(子级工期)i)
     * @param parentItemVO 父级节点
     */
    public static void calculateParentProgress(ScheduleItemVO parentItemVO){
        ScheduleItemVO[] itemVOS = parentItemVO.getChildren();
        if(itemVOS == null || itemVOS.length <= 0){
            return;
        }
        int totalDay = 0;
        int totalPresent = 0;
        for (ScheduleItemVO itemVO : itemVOS) {
            totalDay += itemVO.getPlanDays();
            if(itemVO.getProgressVO() != null){
                totalPresent += itemVO.getProgressVO().getProgress() * itemVO.getPlanDays();
            }
        }

        int currentPresent = 0;
        if (totalDay > 0) {
            currentPresent = (int) Math.floor(totalPresent / totalDay);
        }

        parentItemVO.getProgressVO().setProgress(currentPresent);
    }

    /**
     * 根据子级计算父级进度百分比：
     * 计算方式： 父级进度 = (∑（子级进度 * 工期）i) / (∑(子级工期)i)
     * @param scheduleVO 根结点
     */
    public static void computedParentProgress(ScheduleVO scheduleVO) {
        ScheduleItemVO[] itemVOS = scheduleVO.getScheduleItemVOS();
        if(itemVOS == null || itemVOS.length <= 0){
            return;
        }

        int totalDay = 0;
        int totalPresent = 0;
        for (ScheduleItemVO itemVO : itemVOS) {
            totalDay += itemVO.getPlanDays();
            totalPresent += itemVO.getProgressVO().getProgress() * itemVO.getPlanDays();
        }

        int currentPresent = 0;
        if (totalDay > 0) {
            currentPresent = (int) Math.floor(totalPresent / totalDay);
        }

        scheduleVO.setProgress(currentPresent);
    }

    /**
     * 根据子级计算父级进度百分比：
     * 计算方式： 父级进度 = (∑（子级进度 * 工期）i) / (∑(子级工期)i)
     * @param scheduleBoardVO 根结点
     */
    public static void computedParentProgress(ProjectScheduleBoardVO scheduleBoardVO) {
        List<ScheduleVO> scheduleVOList = scheduleBoardVO.getScheduleVOList();
        if(scheduleVOList == null || scheduleVOList.size() <= 0){
            return;
        }

        int totalDay = 0;
        int totalPresent = 0;
        for (ScheduleVO scheduleVO : scheduleVOList) {
            totalDay += scheduleVO.getPlanDays();
            totalPresent += scheduleVO.getProgress() * scheduleVO.getPlanDays();
        }

        int currentPresent = 0;
        if (totalDay > 0) {
            currentPresent = (int) Math.floor(totalPresent / totalDay);
        }

        scheduleBoardVO.setProgress(currentPresent);
    }
}
